package net.raebiger.spreadsheetcat.spreadsheet;

import java.net.URL;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gdata.client.spreadsheet.FeedURLFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;

public class RetrieveFeedTask extends AsyncTask<Void, Void, SpreadsheetFeed> {

	private final SpreadsheetService service;
	private Exception exception;

	public RetrieveFeedTask(String authToken) {
		service = new SpreadsheetService("SpreadSheetCat");
		// Need to hand over the OAuth2 access token to service
		// header
		service.setHeader("Authorization", "Bearer " + authToken);
	}

	@Override
	protected SpreadsheetFeed doInBackground(Void... params) {
		try {
			// final URL SPREADSHEET_FEED_URL = new
			// URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
			final URL SPREADSHEET_FEED_URL = FeedURLFactory.getDefault().getSpreadsheetsFeedUrl();
			SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
			return feed;
		} catch (Exception e) {
			Log.e("SSC", "Error", e);
			exception = e;
			return null;
		}
	}

	public Exception getException() {
		return exception;
	}

	@Override
	protected void onPostExecute(SpreadsheetFeed result) {
		Log.d("SSC", "onPostExecute");
	}
}